<template>
  <div class="btn-upload-container">
    <div
        class="upload-area"
        :class="{ 'drag-over': isDragOver }"
        @dragover.prevent="handleDragOver"
        @dragleave="handleDragLeave"
        @drop.prevent="handleDrop"
    >
      <input
          type="file"
          ref="fileInput"
          @change="handleFileChange"
          multiple
          style="display: none"
      />
      <p v-if="!files.length" class="upload-prompt">点击或拖拽文件到此区域上传</p>
      <div v-else class="file-list">
        <div v-for="(file, index) in files" :key="index" class="file-item">
          <span class="file-name">{{ file.name }}</span>
          <span class="file-size">({{ formatFileSize(file.size) }})</span>
          <button @click="removeFile(index)" class="remove-btn">×</button>
        </div>
      </div>
    </div>
    <button @click="triggerFileInput" class="select-file-btn">
      选择文件
    </button>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const emit = defineEmits(['files-selected']);

const fileInput = ref(null);
const files = ref([]);
const isDragOver = ref(false);

// 触发文件选择
function triggerFileInput() {
  fileInput.value.click();
}

// 处理文件选择
function handleFileChange(event) {
  const selectedFiles = Array.from(event.target.files);
  files.value = selectedFiles;
  emit('files-selected', files.value);
}

// 处理拖拽文件
function handleDragOver() {
  isDragOver.value = true;
}

function handleDragLeave() {
  isDragOver.value = false;
}

function handleDrop(event) {
  isDragOver.value = false;
  const droppedFiles = Array.from(event.dataTransfer.files);
  files.value = droppedFiles;
  emit('files-selected', files.value);
}

// 移除文件
function removeFile(index) {
  files.value.splice(index, 1);
  emit('files-selected', files.value);
}

// 格式化文件大小
function formatFileSize(bytes) {
  if (bytes === 0) return '0 B';
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
</script>

<style scoped>
.btn-upload-container {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
}

.upload-area {
  border: 2px dashed #ccc;
  border-radius: 8px;
  padding: 20px;
  background-color: #f9f9f9;
  transition: border-color 0.3s ease;
}

.upload-area.drag-over {
  border-color: #007bff;
  background-color: #e6f7ff;
}

.upload-prompt {
  margin: 0;
  color: #666;
  font-size: 14px;
  text-align: center;
}

.file-list {
  margin-top: 10px;
}

.file-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-bottom: 8px;
  background-color: #fff;
}

.file-name {
  flex: 1;
  margin-right: 10px;
  font-size: 14px;
  color: #333;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.file-size {
  font-size: 12px;
  color: #888;
}

.remove-btn {
  background: none;
  border: none;
  color: #ff4d4f;
  cursor: pointer;
  font-size: 16px;
  margin-left: 10px;
}

.select-file-btn {
  margin-top: 20px;
  width: 100%;
  padding: 12px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s ease;
}

.select-file-btn:hover {
  background-color: #0056b3;
}
</style>